/* 
 * File:   flacDecode.h
 * Author: hao
 *
 * Created on 2011年5月10日, 下午5:46
 */

#ifndef FLACDECODE_H
#define	FLACDECODE_H

#include <FLAC/all.h>

class flacDecode {
public:
    flacDecode();
    virtual ~flacDecode();
    
    bool check( void * pData, unsigned long long length );
    void getStatus( unsigned * pFormat, unsigned * pChannels, unsigned * pRate );
    bool Decoding( void * pTarget, size_t length, size_t * ReadSize );
    void getTotalTime( int * pMin, int * pSecond, int * pFrame );
    bool Seek( int Min, int Second, int Frame );
    bool SetTrack( int Min, int Second, int Frame, int EndMin, int EndSecond, int EndFrame );
    
    static FLAC__StreamDecoderReadStatus ReadCallback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
    static FLAC__StreamDecoderSeekStatus SeekCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
    static FLAC__StreamDecoderTellStatus TellCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
    static FLAC__StreamDecoderLengthStatus LengthCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
    static FLAC__StreamDecoderWriteStatus WriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data);
    static void ErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
    static FLAC__bool EofCallback(const FLAC__StreamDecoder *decoder, void *client_data);
    static void MetaCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
    
private:
    
    unsigned long long Length;
    
    char * data;
    
    unsigned long long currentPos;
    
    unsigned long long currentSample;
    
    unsigned long long startSample;
    
    unsigned long long endSample;
    
    FLAC__StreamDecoder* pdecoder;
    
    char * overflowBuffer;
    
    unsigned overflowBufferLength;
    
    char * frameDataBuffer;
    
    unsigned frameDataBufferLength;
    
    unsigned nchannel;
    unsigned nrate;
    unsigned nformat;
    unsigned ntotalsample;

};

#endif	/* FLACDECODE_H */

